package org.sky.framework.security;

import org.sky.framework.security.auth.OAuthUtils;
import org.sky.logger.support.LogFeignTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.lang.Nullable;

import feign.RequestTemplate;

public class AuthFeignTemplate extends LogFeignTemplate {
	private static final Logger logger = LoggerFactory.getLogger(AuthFeignTemplate.class);

	private String channel;
	private DigestSignature digestSignature;

	public AuthFeignTemplate(@Nullable String channel, @Nullable DigestSignature digestSignature) {
		this.channel = channel;
		this.digestSignature = digestSignature;
	}

	@Override
	public void apply(RequestTemplate template) {
		super.apply(template);
		AuthEntity authEntity = new AuthEntity();
		authEntity.setIsd(OAuthUtils.True);
		authEntity.setChn(channel);
		try {
			String accessToken = digestSignature.encode(authEntity);
			template.header(AuthUtils.Iauthorization, accessToken);
		} catch (Exception e) {
			logger.error("AuthRestTemplate digestSignature encode token error", e);
		}

	}

}
